import os.path

from flask import Flask, Blueprint
from PyPDF2 import PdfMerger

# 常量
BASE_DIR_PDF_SOURCE = os.path.join("D:\work\python\docFile","doc\pdf\source")
BASE_DIR_PDF_OUTPUT = os.path.join("D:\work\python\docFile","doc\pdf\output")

# 创建 Blueprint
nw_pdf_blueprint = Blueprint('nw_pdf', __name__)

# 获取所有记录
@nw_pdf_blueprint.route('/api/merge', methods=['GET'])
def pdf_merge_controller():
    pdf1 = os.path.join(BASE_DIR_PDF_SOURCE, "打印（公开）.pdf")
    pdf2 = os.path.join(BASE_DIR_PDF_SOURCE, "打印2（公开）.pdf")

    # 定义要合并的PDF文件列表
    pdf_files = [pdf1, pdf2]
    # 创建一个PdfMerger对象
    merger = PdfMerger()
    # 遍历文件列表，将每个PDF文件添加到merger对象中
    for pdf_file in pdf_files:
        merger.append(pdf_file)

    # 将合并后的PDF文件保存到输出文件中
    merger.write(os.path.join(BASE_DIR_PDF_OUTPUT,"merged_output.pdf"))

    # 关闭merger对象
    merger.close()

    print("PDF文件合并完成！")

    return "PDF文件合并完成！"